// xc_lshop/business/pack/writeoff.js
var actbill = require('../model/business_bill')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],


  /**
   * 页面的初始数据
   */
  data: {
    show:true,
    xcmodel:null,
    allnum:0,
    checking:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  xcloaddata: function (options, config) {
    
    this.opscan()
    // this.getmodel('12')

    //showmodel
    //加载数据完成
  },
  opscan:function(e){
    var that= this;
      wx.scanCode({
      onlyFromCamera: false,
      success:function(reslut){
        var vcode=""
        if(reslut.result){
          vcode=reslut.result
        }
        else if( reslut.path){
          vcode=reslut.path        
        }
        if(vcode)
        {
          //"0022", "2020202011112626112506"
          //that.deailerdata("0021_2020202011112323195147") 
          // that.deailerdata("0022_2121212103031010180111") 
          that.deailerdata(vcode) 
        }else{
        

          wx.showModal({
            title: '提示',
            content: '未识别到核销的套餐卡',
            confirmText:"重新扫描",
            success (res) {
              if (res.confirm) {
                that.opscan()
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })

         
        }          
      },
      fail:function(res){
        

        wx.navigateBack({
          delta: 1,
        })
        
       
      },
      complete:function(com){
      
        
      }
    })  
  },
  deailerdata(ordernum){
    var that = this;
    that.getmodel(ordernum)
  },
  getmodel: function (packlistid) {
    var that = this;
    actbill.getpackmodel(packlistid).then(function (resinfo) {
      if (resinfo["status"] == "1") {
        var xcmodel = resinfo["obj"];
        console.log(xcmodel)  
        that.setData({
          xcmodel:xcmodel
        })
       }else{
        wx.showModal({
          title: '提示',
          content: '未识别到核销的套餐卡',
          confirmText:"重新扫描",
          success (res) {
            if (res.confirm) {
              that.opscan()
            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })

       }
    })
  },
  onChangeItem(e){
    console.log(e)
    console.log(this.data.xcmodel)

    var index=e.currentTarget.dataset.index
    console.log(index)

    var name='xcmodel.proinfo['+index+'].choosed';
    var namenum='xcmodel.proinfo['+index+'].choosednum';

    var choosednum=1;
    var choosedstatus=true;
    console.log(typeof(e.detail));
    // return

    if(typeof(e.detail)=='number'){
      choosednum=e.detail;
      if(choosednum==0){
        choosedstatus=false
      }
    }else if(this.data.xcmodel.proinfo[index] && this.data.xcmodel.proinfo[index].choosednum){
      choosednum=this.data.xcmodel.proinfo[index].choosednum

      choosedstatus=!this.data.xcmodel.proinfo[index].choosed
   
    }
    
    this.setData({
      [name]:choosedstatus,
      [namenum]:choosednum
    })
  
    this.changeall()
  },
  changeall(){
    var proinfo=this.data.xcmodel.proinfo
    console.log("proinfo")
    console.log(proinfo)
    var choosednum=0;
    var allnum=0;

    var failnum=0
    for(var i in proinfo){
      if((proinfo[i].allnum - proinfo[i].usednum) > 0){
        if(proinfo[i].choosed){
          choosednum ++
          if(proinfo[i].choosednum){
            allnum +=proinfo[i].choosednum
          }
        }
      }else{
        failnum ++
      }
     
      
    }
    var update={}
    update['allnum']=allnum;
    update['choosednum']=choosednum;

  
    if(choosednum==(proinfo.length - failnum)){
      if(!this.data.allchecked){
        update['allchecked']=true
      }
      
    }else{
     

      if(this.data.allchecked){
        update['allchecked']=false
      }
    }

    this.setData(update)
  },
  exchange(){
    var that=this;
    console.log("this.data.xcmodel.proinfo")
    console.log(this.data.xcmodel.proinfo)
    if(this.data.allnum<=0){
      wx.showToast({
        title: '请至少选择一件',
      })
      return false
    }
    this.setData({
      checking:true
    })
    var proinfo=this.data.xcmodel.proinfo

    var cart=[]
    for(var i in proinfo){
      if(proinfo[i].choosed){
        var model={
          id:proinfo[i]['id'],
          proid:proinfo[i]['proid'],
          name:proinfo[i]['name'],
          proinfo:proinfo[i]['proinfo'],
          num: proinfo[i].choosednum,
          pro_ident: proinfo[i].pro_ident,

        };
        cart.push(model)
      }
    }
  
    actbill.checkpack(this.data.xcmodel.id,cart).then(function (res) {
      that.setData({
        checking:false
      })
       if(res['status']==1){
         wx.showToast({
           icon:'none',
           title: '核销成功',
         })
         that.getmodel(that.data.xcmodel.id)
       }else{
        wx.showToast({
          icon:'error',
          title: '核销失败',
        })
       }

    })

   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})